#include "stdafx.h"
#include "Hooks.h"
#include "RecordingSession.h"
#include "PlaybackSession.h"

extern CPlaybackSession *g_pPlaybackSession;
extern CRecordingSession *g_pRecordingSession;

LRESULT CALLBACK GetMsgProc(int code, WPARAM wparam, LPARAM lparam)
{
	//This hook is needed to respond to Ctrl+Break or Ctrl+Esc or Ctrl+Alt+Del
	//which is used when the user wants to stop recording or playing back.
	//The system removes the hook anyway, but WinMacro will not be aware of that
	//so we need to watch for all messages and see if WM_CANCELJOURNAL is sent.
	//If so, we need to change our UI state to indicate recording/playback has 
	//been cancelled. I've also removed the hook, but itsn't necessary, the system
	//does it.
	Session *pSession = NULL;
	
	if (g_pRecordingSession)
		pSession = g_pRecordingSession;
	else
		pSession = g_pPlaybackSession;

	if (code==HC_ACTION)
	{
		MSG *msg = (MSG *)lparam;

		if (msg->message == WM_CANCELJOURNAL)
		{	
			pSession->SessionCancelled();
		}
	}

	LRESULT val = CallNextHookEx(pSession->GetGetMessageHookHandle(), code, wparam, lparam);

	return val;
}
